package cn.xiongxf.result;

import cn.xiongxf.exception.GlobalErrorCode;
import lombok.Data;

// 返回JSON结果
@Data
// 建造者模式
//@Builder
public class JsonResult {

    private boolean success = true;

    // 错误码，用来描述错误类型 ，1000 表示没有错误
    private String code = "1000";

    private String message = "成功";


    // 返回的数据对象
    private Object data;

    public static JsonResult success(){
        return new JsonResult();
    }

    public static JsonResult success(Object obj){
        JsonResult instance = new JsonResult();
        instance.setData(obj);
        return instance;
    }

    public static JsonResult success(Object obj, String code){
        JsonResult instance = new JsonResult();
        instance.setCode(code);
        instance.setData(obj);
        return instance;
    }

    public static JsonResult error(String code, String message){
        JsonResult instance = new JsonResult();
        instance.setSuccess(false);
        instance.setMessage(message);
        instance.setCode(code);
        return instance;
    }

    public static JsonResult error(){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(false);
        return jsonResult;
    }

    public static JsonResult error(String message){
        return error(null,message);
    }

    // 枚举异常类有参构造
    public static JsonResult error(GlobalErrorCode globalErrorCode){
       return error(globalErrorCode.getCode(),globalErrorCode.getMessage());
    }

}
